﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HKEA.CMS.Core.BLL;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using System.Web.SessionState;
using HKEA.CMS.Core.BLL.Tbl;
using HKEA.CMS.Core.BLL.App;
namespace WebApplication.ServerAshxFile
{
    /// <summary>
    /// DeleteContactPerson 的摘要说明
    /// </summary>
    public class DeleteContactPerson : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            string str = string.Empty;
            context.Response.ContentType = "text/plain";
            string strImgID = context.Request.QueryString["UID"].ToString();
            string[] uid = strImgID.Split('-');  
            tblCust_ContactPerson cust_cpENT = new tblCust_ContactPerson();
            tblCust_ContactPersonBLL cust_cpBLL = new tblCust_ContactPersonBLL();
            //if() 满足权限
            cust_cpENT.CustomerUID = Convert.ToInt32(uid[0]);
            cust_cpENT.ContactPersonUID=Convert.ToInt32(uid[1]);
            cust_cpENT.Deleted = 0;
            cust_cpENT = cust_cpBLL.GetEntity(cust_cpENT);
            if (cust_cpENT != null)
            {
                cust_cpENT.Deleted = 1;
                cust_cpENT.LastModifyDate = DateTime.Now;
                cust_cpENT.LastModifyBy = GetManageName(context);
                if (cust_cpBLL.UpdateENT(cust_cpENT) > 0)
                {
                    str = "{\"Issucess\":\"1\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200011") + "\"}";
                    WriteToLoge(Convert.ToInt32(cust_cpENT.CustomerUID),cust_cpENT.ContactPersonUID);
                }
                else
                {
                    str = "{\"Issucess\":\"0\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200010") + "\"}";
                }
            }
            else
            {
                str = "{\"Issucess\":\"0\",\"Meg\":\"" + ResourceManage.GetResourceString("msg200013") + "\"}";
            }
            context.Response.Write(str);
        }
        public string GetManageName(HttpContext context)
        {
            string ret = string.Empty;
            if (context.Session[Constant.SessionName.Manage] != null)
            {
                ret = (context.Session[Constant.SessionName.Manage] as tblUser).User_Name;
            }
            else
            { ret = "system"; }
            return ret;
        }

        protected void WriteToLoge(int customerUID,int CPersonUID)
        {
            string name = string.Empty;
            
            tblHistory entlog = new tblHistory();
            BasePage page = new BasePage();
            tblCustomer Company = tblCustomerBLL.GetInstance().GetEntity(customerUID);
            tblContactPerson entPerson = new tblContactPerson();
            entPerson.ContactPersonUID = CPersonUID;
            entPerson = tblContactPersonBLL.GetInstance().GetEntity(entPerson);
            
            tblUser ent = page.GetManageFromSession();
            entlog.HistoryCatUID = 2;
            entlog.HistoryTypeUID = 6;
            entlog.CustomerUID = customerUID;
            entlog.Cust_MemberUID = 0;
            entlog.ContactPersonUID = 0;
            entlog.History_Detail = ent.User_Name + " {" + Company.Name_Eng + "}{ContactPerson}hava Delete " +entPerson.LastName_Eng + entPerson.FirstName_Eng + " information";
            entlog.LastModifyBy = ent.User_Name;
            int tal = HKEA.CMS.Core.BLL.Tbl.tblHistoryBLL.GetInstance().InsertENT(entlog);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}